package com.geeksaga.dao.ibatis.mysql;

import java.util.List;


import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;

import com.geeksaga.dao.TagDao;
import com.geeksaga.dao.ibatis.BaseIBatisDao;
import com.geeksaga.domain.Tag;

/**
 * @author newbie
 * @since 0.1
 */
@Repository
public class TagDaoImpl extends BaseIBatisDao implements TagDao {

    /* (non-Javadoc)
     * @see com.geeksaga.dao.TagDao#add(com.geeksaga.domain.Tag)
     */
    public Tag add(Tag tag) throws DataAccessException {
        return (Tag) getSqlMapClientTemplate().insert("TAG.add", tag);
    }

    /* (non-Javadoc)
     * @see com.geeksaga.dao.TagDao#findByTagName(com.geeksaga.domain.Tag)
     */
    public Tag findByTagName(Tag tag) throws DataAccessException {
        return (Tag) getSqlMapClientTemplate().queryForObject("TAG.find", tag);
    }
    
    /* (non-Javadoc)
     * @see com.geeksaga.dao.TagDao#exists(com.geeksaga.domain.Tag)
     */
    @SuppressWarnings("unchecked")
    public boolean exists(Tag tag) throws DataAccessException {
        List<Tag> list = (List<Tag>) getSqlMapClientTemplate().queryForList("TAG.find", tag);
        
        if(list == null || (list != null && list.size() == 0))
            return false;
        else
            return true;
    }

    /* (non-Javadoc)
     * @see com.geeksaga.dao.TagDao#updateCnt(com.geeksaga.domain.Tag)
     */
    public int updateCnt(Tag tag) throws DataAccessException {
        return getSqlMapClientTemplate().update("TAG.updateCnt", tag);
    }
}